/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UpdateTutee.java
 *
 * Created on Jul 21, 2012, 10:38:13 PM
 */
package monitor;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author FrostBiteX77
 */
public class UpdateTutee extends javax.swing.JFrame {
    
    String TuteeID; 
    String fullName;
    private Component frame;
    private Container c;
    
    /** Creates new form UpdateTutee */
    public UpdateTutee() {
        initComponents();
        c = getContentPane();
        c.setBackground(Color.white);
    }
    
    
    public void Update(){
        
        DBManager dbConnector = new DBManager();
        QHandler qhandler = new QHandler();
        qhandler.getTuteeInfo(TuteeID);
        dbConnector.Connect();
        dbConnector.getInfo(qhandler.query);
        
        DBManager dbConnector2 = new DBManager();
        QHandler qhandler2 = new QHandler();
        qhandler2.getTuteeContact(TuteeID);
        dbConnector2.Connect();
        dbConnector2.getInfo(qhandler2.query);
        
        try {            
            fNameTxt.setText(dbConnector.rs.getString("fName"));            
            mNameTxt.setText(dbConnector.rs.getString("mName"));
            lNameTxt.setText(dbConnector.rs.getString("lName"));
            idNumTxt.setText(dbConnector.rs.getString("idNum"));
            degreeProgramTxt.setText(dbConnector.rs.getString("degreeProgram"));
            CGPATxt.setText(dbConnector.rs.getString("CGPA"));
            eaddTxt.setText(dbConnector.rs.getString("emailAdd"));
            contactTxt.setText(dbConnector2.rs.getString("contactNum"));
            remarkTxt.setText(dbConnector.rs.getString("remark"));
        } catch (SQLException ex) {
            
        }
        
          
        dbConnector.Disconnect(dbConnector.rs);
        dbConnector2.Disconnect(dbConnector2.rs);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        fNameTxt = new javax.swing.JTextField();
        mNameTxt = new javax.swing.JTextField();
        lNameTxt = new javax.swing.JTextField();
        idNumTxt = new javax.swing.JTextField();
        degreeProgramTxt = new javax.swing.JTextField();
        BtnCancel = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        CGPATxt = new javax.swing.JTextField();
        BtnUpdate = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        eaddTxt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        contactTxt = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        remarkTxt = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Update Tutee Profile");

        jLabel1.setText("Name");

        jLabel4.setText("ID Number");

        jLabel6.setText("Degree Program");

        BtnCancel.setFont(new java.awt.Font("Modern No. 20", 0, 18));
        BtnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/Cancel.png"))); // NOI18N
        BtnCancel.setText("Cancel");
        BtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelActionPerformed(evt);
            }
        });

        jLabel2.setText("CGPA");

        BtnUpdate.setFont(new java.awt.Font("Modern No. 20", 0, 18));
        BtnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/update.png"))); // NOI18N
        BtnUpdate.setText("Update");
        BtnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnUpdateActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 13));
        jLabel5.setText("Update Profile");

        jLabel7.setText("Email");

        jLabel3.setText("Contact No.");

        jLabel8.setText("Remarks");

        jLabel11.setText("First Name");

        jLabel12.setText("Middle Name");

        jLabel13.setText("Last Name");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(BtnUpdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BtnCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(remarkTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel11))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(idNumTxt, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(fNameTxt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
                                        .addComponent(degreeProgramTxt, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(eaddTxt, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(contactTxt, javax.swing.GroupLayout.Alignment.LEADING)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(17, 17, 17)
                                                .addComponent(jLabel12))
                                            .addComponent(mNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(10, 10, 10)
                                                .addComponent(jLabel13))
                                            .addComponent(lNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(CGPATxt, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                .addGap(19, 19, 19))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idNumTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(CGPATxt, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(degreeProgramTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(eaddTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(contactTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(remarkTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BtnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void BtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelActionPerformed
// TODO add your handling code here:
    ViewTutee tutee = new ViewTutee();
    tutee.setVisible(true);
    tutee.setLocationRelativeTo(null);
    this.dispose();
}//GEN-LAST:event_BtnCancelActionPerformed

private void BtnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnUpdateActionPerformed
// TODO add your handling code here:
    
    DBManager dbConnector3 = new DBManager();
    QHandler qhandler3 = new QHandler();
    qhandler3.getTutoringSchedIDtutee(TuteeID);
    dbConnector3.Connect();
    dbConnector3.getInfo(qhandler3.query);
        
    DBManager con = new DBManager();
    con.Connect();
    
    DBManager con2 = new DBManager();
    con2.Connect();
    
    DBManager con3 = new DBManager();
    con3.Connect();
    
    fullName = fNameTxt.getText() + ' ' + ',' + mNameTxt.getText() + ' ' + lNameTxt.getText();
    
    Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
    Matcher m = p.matcher(eaddTxt.getText());
    boolean matchFound = m.matches();
    
    /* Error checking mechanism */
     if((idNumTxt.getText().equals("")) 
            || (fNameTxt.getText().equals(""))
            || (lNameTxt.getText().equals("")) 
            || (degreeProgramTxt.getText().equals(""))
            || (eaddTxt.getText().equals(""))
            || (contactTxt.getText().equals(""))
            || (CGPATxt.getText().equals(""))){
        
        JOptionPane.showMessageDialog(frame,
        "Please Complete all Fields.",
        "Incomplete",
        JOptionPane.WARNING_MESSAGE);
    }
      else if(!(Pattern.matches("([a-zA-Z]+\\s+)*[a-zA-Z]+", fNameTxt.getText())) ||
            !(Pattern.matches("([a-zA-Z]+\\s+)*[a-zA-Z]+", lNameTxt.getText()))){
        
        JOptionPane.showMessageDialog(frame,
        "Tutee name should only contain valid characters.",
        "Invalid",
        JOptionPane.WARNING_MESSAGE);
    }
    
    else if(!(degreeProgramTxt.getText().equals("CS-NE"))
             && !(degreeProgramTxt.getText().equals("CS-CSE"))
             && !(degreeProgramTxt.getText().equals("CS-ST"))
             && !(degreeProgramTxt.getText().equals("CS-IST"))
             && !(degreeProgramTxt.getText().equals("INSYS"))
             ){
               
        JOptionPane.showMessageDialog(frame,
        "Invalid Degree Program.",
        "Invalid",
        JOptionPane.WARNING_MESSAGE);
         
     }
    
    else if(Float.parseFloat(CGPATxt.getText()) < 0 || Float.parseFloat(CGPATxt.getText()) > 4){
        JOptionPane.showMessageDialog(frame,
        "Make sure CGPA is between 0.0 and 4.0.",
        "Invalid",
        JOptionPane.WARNING_MESSAGE);
    }
    
    
    
    else if(idNumTxt.getText().length() != 8){
        JOptionPane.showMessageDialog(frame,
        "ID number should be 8 digits.",
        "Invalid",
        JOptionPane.WARNING_MESSAGE);
    }
    
    else if(!matchFound){
         JOptionPane.showMessageDialog(frame,
        "Invalid E-mail Address.",
        "Invalid",
        JOptionPane.WARNING_MESSAGE);
    }
     else {
         
          QHandler handler = new QHandler();
          QHandler handler2 = new QHandler();
          QHandler handler3 = new QHandler();
          
          handler.updateTuteeInfo(TuteeID, idNumTxt.getText(), fNameTxt.getText(), mNameTxt.getText(), lNameTxt.getText(), degreeProgramTxt.getText(), CGPATxt.getText(), eaddTxt.getText(), remarkTxt.getText());
           con.Push(handler.query);
           con.Disconnect(con.rs);
           
           handler2.updateTuteeContact(TuteeID, idNumTxt.getText(), contactTxt.getText());
           con2.Push(handler2.query);
           con2.Disconnect(con2.rs);
           
            try{
          System.out.println(dbConnector3.rs.getString("schedNum"));
          
           
           do{
                         
           handler3.updateSchedInfo3(dbConnector3.rs.getString("schedNum"), idNumTxt.getText(), fullName);
           
           con3.Push(handler3.query);
    
           }while(dbConnector3.rs.next());
           }catch(SQLException ex){}
            
            con3.Disconnect(con3.rs);
           
            ViewTutee tutee = new ViewTutee();
            tutee.setVisible(true);
            tutee.setLocationRelativeTo(null);
            this.dispose();
           
           
         
     }
}//GEN-LAST:event_BtnUpdateActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UpdateTutee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UpdateTutee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UpdateTutee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UpdateTutee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UpdateTutee().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnCancel;
    private javax.swing.JButton BtnUpdate;
    private javax.swing.JTextField CGPATxt;
    private javax.swing.JTextField contactTxt;
    private javax.swing.JTextField degreeProgramTxt;
    private javax.swing.JTextField eaddTxt;
    private javax.swing.JTextField fNameTxt;
    private javax.swing.JTextField idNumTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField lNameTxt;
    private javax.swing.JTextField mNameTxt;
    private javax.swing.JTextField remarkTxt;
    // End of variables declaration//GEN-END:variables
}
